package com.chinasofti.dao;

import com.chinasofti.util.ConnectionFactory;
import com.chinasofti.vo.Department;
import com.chinasofti.vo.Employee;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DepartmentDao {
    private Connection conn;

    public List<Department> selectAll() {
        conn = ConnectionFactory.getConnection();
        List<Department> departmentsList = new ArrayList<>();
        PreparedStatement pst = null;
        ResultSet rs = null;
        String sql = "select * from department";
        Department department = null;

        try {
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()) {
                department = new Department();
                department.setDepartmentid(rs.getInt("departmentid"));
                department.setDepartmentname(rs.getString("departmentname"));
                departmentsList.add(department);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionFactory.closeConnection();
        }
        return departmentsList;
    }

    public Employee selectByUsername(String username) {
        Employee employee = null;
        conn = ConnectionFactory.getConnection();
        PreparedStatement pst = null;
        String sql = "select * from employee where username=?";
        ResultSet rs = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, username);
            rs = pst.executeQuery();
            if (rs.next()) {
                employee = new Employee();
                employee.setEmployeeid(rs.getInt("employeeid"));
                employee.setEmployeename(rs.getString("employeename"));
                employee.setUsername(rs.getString("username"));
                employee.setPhone(rs.getString("phone"));
                employee.setEmail(rs.getString("email"));
                employee.setStatus(rs.getString("status"));
                employee.setDepartmentid(rs.getInt("departmentid"));
                employee.setPassword(rs.getString("password"));
                employee.setRole(rs.getString("role"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionFactory.closeConnection();
        }
        return employee;
    }

    public void insert(String departmentname) {
        conn = ConnectionFactory.getConnection();
        PreparedStatement pst = null;
        String sql = "insert into department (departmentname) values(?)";
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1,departmentname);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.closeConnection();
        }

    }

    public void delete(Integer departmentid) {
        conn = ConnectionFactory.getConnection();
        PreparedStatement pst = null;
        String sql = "delete from department where departmentid=?";
        try {
            pst = conn.prepareStatement(sql);
            pst.setInt(1,departmentid);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.closeConnection();
        }
    }
    public void updateNameById(int departmentid, String departmentname) {
        conn = ConnectionFactory.getConnection();
        PreparedStatement pst = null;
        String sql = "update department set departmentname=? where departmentid=?";
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1,departmentname);
            pst.setInt(2,departmentid);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.closeConnection();
        }
    }
}
